//
//  M4UploadController2.swift
//  M4
//
//  Created by YuChuang on 2025/1/25.
//

import UIKit

class M4UploadController2: M4BaseViewController {

    @IBOutlet weak var M4ScrollView: UIScrollView!
    
    var M4UploadResult: ((Bool)->Void)?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        M4BackIcon = "icon_back_black"
        M4NavTitle(M4Title: "Seguridad de los datos", M4NavTitleColor: COLOR_333333)
        self.view.backgroundColor = .white
        
        M4View.M4DecriptionView.c = self
        M4View.M4DecriptionView.M4BtnSelectedStatus = { r in
            if r == true {
                self.M4View.M4ConfirmarButton.backgroundColor = COLOR_00479D
                self.M4View.M4ConfirmarButton.isEnabled = true
            } else {
                self.M4View.M4ConfirmarButton.backgroundColor = COLOR_EBEBEB
                self.M4View.M4ConfirmarButton.isEnabled = false
            }
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        M4NavBarColor = .white
    }
    override func M4BackClick(sender: UIButton) {
        for c in navigationController?.children ?? [] {
            if c is M4FirstInfoViewController {
                AppDelegate.keyWindow()?.rootViewController = M4TabBarViewController()
                return
            }
        }
        self.navigationController?.popViewController(animated: true)
    }
    
    @objc func M4RechazarClicked(_ sender: Any) {
        for c in navigationController?.children ?? [] {
            if c is M4FirstInfoViewController {
                AppDelegate.keyWindow()?.rootViewController = M4TabBarViewController()
                return
            }
        }
        self.navigationController?.popViewController(animated: true)
    }
    
    @objc func M4ConfirmarClicked(_ sender: Any) {
        M4View.M4ConfirmarButton.M4EventUnavailable = false
        
      M4UserDataManager.M4HasShowUploadPage = true
        
        M4PublicNetwork.M4ShowUploadAlert { r in
            self.navigationController?.popViewController(animated: true)
            self.M4UploadResult?(r)
        }
    }
    
    lazy var M4View: M4UploadView = {
        let V = M4UploadView.view()
        V.M4RechazarButton.addTarget(self, action: #selector(M4RechazarClicked(_:)), for: .touchUpInside)
        V.M4ConfirmarButton.addTarget(self, action: #selector(M4ConfirmarClicked(_:)), for: .touchUpInside)
        M4ScrollView.addSubview(V)
        V.snp.makeConstraints { make in
            make.left.top.bottom.equalToSuperview()
            make.width.equalTo(M4ScreenWidth)
        }
        return V
    }()

}
